package net.pws.oos.web.dto;

import java.util.ArrayList;
import java.util.List;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.IndicatorValue;
import net.pws.oos.biz.model.Station;
import net.pws.oos.biz.model.StationRecord;
import net.pws.oos.biz.model.User;

import org.apache.commons.lang.StringUtils;

public class StationDto extends Station {

	private static final long serialVersionUID = 1L;

	public static List<StationDto> from(List<Station> stationList) {
		List<StationDto> dto = new ArrayList<StationDto>();
		for (Station station : stationList) {
			dto.add(from(station));
		}
		return dto;
	}

	public static StationDto from(Station station) {
		if (station == null) {
			station = new Station();
		}
		String[] ignoredProps = { 
				"creator", 
				"deletor", 
				"stationRecord", 
				"stationType", 
				"siteProperties", 
				"propertyOwnership", 
				"areaType", 
				"constructionModel", 
				"siteValid"
				};

		StationDto dto = new StationDto();
		BeanUtils.copyProperties(dto, station, ignoredProps);

		User creator = station.getCreator();
		if (creator != null) {
			dto.setCreatorId(creator.getId());
			dto.setCreatorName(creator.getName());
		}
		
		User deletor = station.getDeletor();
		if (deletor != null) {
			dto.setDeletorId(deletor.getId());
			dto.setDeletorName(deletor.getName());
		}

		StationRecord stationRecord = station.getStationRecord();
		if (stationRecord != null) {
			dto.setStationRecordId(stationRecord.getId());
		}

		IndicatorValue stationType = station.getStationType();
		if (stationType != null) {
			dto.setStationTypeId(stationType.getId());
			dto.setStationTypeName(stationType.getName());
		}
		IndicatorValue siteProperties = station.getSiteProperties();
		if (siteProperties != null) {
			dto.setSitePropertiesId(siteProperties.getId());
			dto.setSitePropertiesName(siteProperties.getName());
		}
		IndicatorValue propertyOwnership = station.getPropertyOwnership();
		if (propertyOwnership != null) {
			dto.setPropertyOwnershipId(propertyOwnership.getId());
			dto.setPropertyOwnershipName(propertyOwnership.getName());
		}
		IndicatorValue areaType = station.getAreaType();
		if (areaType != null) {
			dto.setAreaTypeId(areaType.getId());
			dto.setAreaTypeName(areaType.getName());
		}
		IndicatorValue constructionModel = station.getConstructionModel();
		if (constructionModel != null) {
			dto.setConstructionModelId(constructionModel.getId());
			dto.setConstructionModelName(constructionModel.getName());
		}
		IndicatorValue siteValid = station.getSiteValid();
		if (siteValid != null) {
			dto.setSiteValidId(siteValid.getId());
			dto.setSiteValidName(siteValid.getName());
		}

		return dto;
	}

	public static Station to(Station station, StationDto dto, EntityManager entityManager) {
		if (station == null) {
			station = new Station();
		}

		String[] ignoredProps = { 
				"id",
				"creator", 
				"createDateTime",
				"deleted",
				"deletor", 
				"deleteDateTime",
				"stationRecord",
				"stationType",
				"siteProperties",
				"propertyOwnership",
				"areaType",
				"constructionModel",
				"siteValid" };
		BeanUtils.copyProperties(station, dto, ignoredProps);

//		if (StringUtils.isEmpty(dto.getCreatorId())) {
//			station.setCreator(null);
//		} else {
//			station.setCreator(entityManager.find(User.class, dto.getCreatorId()));
//		}

		if (StringUtils.isEmpty(dto.getStationTypeId())) {
			station.setStationType(null);
		} else {
			station.setStationType(entityManager.find(IndicatorValue.class,
					dto.getStationTypeId()));
		}
		if (StringUtils.isEmpty(dto.getSitePropertiesId())) {
			station.setSiteProperties(null);
		} else {
			station.setSiteProperties(entityManager.find(IndicatorValue.class,
					dto.getSitePropertiesId()));
		}
		if (StringUtils.isEmpty(dto.getPropertyOwnershipId())) {
			station.setPropertyOwnership(null);
		} else {
			station.setPropertyOwnership(entityManager.find(IndicatorValue.class,
					dto.getPropertyOwnershipId()));
		}
		if (StringUtils.isEmpty(dto.getAreaTypeId())) {
			station.setAreaType(null);
		} else {
			station.setAreaType(entityManager.find(IndicatorValue.class,
					dto.getAreaTypeId()));
		}
		if (StringUtils.isEmpty(dto.getConstructionModelId())) {
			station.setConstructionModel(null);
		} else {
			station.setConstructionModel(entityManager.find(IndicatorValue.class,
					dto.getConstructionModelId()));
		}
		if (StringUtils.isEmpty(dto.getSiteValidId())) {
			station.setSiteValid(null);
		} else {
			station.setSiteValid(entityManager.find(IndicatorValue.class,
					dto.getSiteValidId()));
		}

		return station;
	}

	private String creatorId;
	private String creatorName;
	private String deletorId;
	private String deletorName;
    private List<AttachmentDto> attachments;
	private String stationRecordId;
	private String stationTypeId;
	private String stationTypeName;
	private String sitePropertiesId;
	private String sitePropertiesName;
	private String propertyOwnershipId;
	private String propertyOwnershipName;
	private String areaTypeId;
	private String areaTypeName;
	private String constructionModelId;
	private String constructionModelName;
	private String siteValidId;
	private String siteValidName;

	public String getCreatorId() {
		return creatorId;
	}

	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}

	public String getCreatorName() {
		return creatorName;
	}

	public void setCreatorName(String creatorName) {
		this.creatorName = creatorName;
	}

	public String getDeletorId() {
		return deletorId;
	}

	public void setDeletorId(String deletorId) {
		this.deletorId = deletorId;
	}

	public String getDeletorName() {
		return deletorName;
	}

	public void setDeletorName(String deletorName) {
		this.deletorName = deletorName;
	}

	public List<AttachmentDto> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<AttachmentDto> attachments) {
		this.attachments = attachments;
	}

	public String getStationRecordId() {
		return stationRecordId;
	}

	public void setStationRecordId(String stationRecordId) {
		this.stationRecordId = stationRecordId;
	}

	public String getStationTypeId() {
		return stationTypeId;
	}

	public void setStationTypeId(String stationTypeId) {
		this.stationTypeId = stationTypeId;
	}

	public String getStationTypeName() {
		return stationTypeName;
	}

	public void setStationTypeName(String stationTypeName) {
		this.stationTypeName = stationTypeName;
	}

	public String getSitePropertiesId() {
		return sitePropertiesId;
	}

	public void setSitePropertiesId(String sitePropertiesId) {
		this.sitePropertiesId = sitePropertiesId;
	}

	public String getSitePropertiesName() {
		return sitePropertiesName;
	}

	public void setSitePropertiesName(String sitePropertiesName) {
		this.sitePropertiesName = sitePropertiesName;
	}

	public String getPropertyOwnershipId() {
		return propertyOwnershipId;
	}

	public void setPropertyOwnershipId(String propertyOwnershipId) {
		this.propertyOwnershipId = propertyOwnershipId;
	}

	public String getPropertyOwnershipName() {
		return propertyOwnershipName;
	}

	public void setPropertyOwnershipName(String propertyOwnershipName) {
		this.propertyOwnershipName = propertyOwnershipName;
	}

	public String getAreaTypeId() {
		return areaTypeId;
	}

	public void setAreaTypeId(String areaTypeId) {
		this.areaTypeId = areaTypeId;
	}

	public String getAreaTypeName() {
		return areaTypeName;
	}

	public void setAreaTypeName(String areaTypeName) {
		this.areaTypeName = areaTypeName;
	}

	public String getConstructionModelId() {
		return constructionModelId;
	}

	public void setConstructionModelId(String constructionModelId) {
		this.constructionModelId = constructionModelId;
	}

	public String getConstructionModelName() {
		return constructionModelName;
	}

	public void setConstructionModelName(String constructionModelName) {
		this.constructionModelName = constructionModelName;
	}

	public String getSiteValidId() {
		return siteValidId;
	}

	public void setSiteValidId(String siteValidId) {
		this.siteValidId = siteValidId;
	}

	public String getSiteValidName() {
		return siteValidName;
	}

	public void setSiteValidName(String siteValidName) {
		this.siteValidName = siteValidName;
	}
	
	
}
